//
//  HeroInformationDetailViewController.m
//  LOLProject
//
//  Created by lanou3g on 15/6/5.
//  Copyright (c) 2015年 lanou3g.com蓝欧科技. All rights reserved.
//

#import "HeroInformationDetailViewController.h"

@interface HeroInformationDetailViewController ()

@end

@implementation HeroInformationDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
//    设置背景颜色
    self.view.backgroundColor=[UIColor orangeColor];
//    设置返回UIBarButtonItem
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(goBackToHeroInformation)];
//    创建introductionView并作为默认视图
    [self createIntroductionView];
//    创建skillView,暂不使用
    [self createSkillView];
//    createTacticScrollView
    [self createTacticScrollView];
//    默认显示最新视频
    self.heroInformationSegmentControl.selectedSegmentIndex=0;
    [self select:self.heroInformationSegmentControl];
//    从网上那个请求数据
    [self requestHeroInformationDetailData];
}

#pragma mark---实现给返回UIBarButtonItem添加的方法
- (void) goBackToHeroInformation{
    [self dismissViewControllerAnimated:YES completion:nil];
    return ;
}

#pragma mark---懒加载英雄信息字典heroInformationDataDic
- (NSMutableDictionary *) heroInformationDataDic{
    if (_heroInformationDataDic==nil) {
        self.heroInformationDataDic=[NSMutableDictionary dictionary];
    }
    return _heroInformationDataDic;
}

#pragma mark---从网上那个请求数据
- (void) requestHeroInformationDetailData{
//    异步连接下的GET请求
//    1.创建网址对象
    NSURL * url=[NSURL URLWithString:[NSString stringWithFormat:kHeroInformation_URL,self.hero.enName]];
//    2.创建网络请求对象
    NSURLRequest * request=[NSURLRequest requestWithURL:url];
//    3.创建连接对象,并发送请求
    __weak HeroInformationDetailViewController * heroInformationDetailVC=self;
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
//        代码保护
        if (data==nil) {
            return ;
        }
//        4.解析数据
        NSDictionary * sourceDic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//        处理数据(封装model对象)
//        ①.处理基本信息
        HeroInformation * heroInformation=[[HeroInformation alloc] init];
        [heroInformation setValuesForKeysWithDictionary:sourceDic];
        [heroInformationDetailVC.heroInformationDataDic setObject:heroInformation forKey:@"heroInformation"];
//        ②.处理技能信息
        SkillInformation * skill_B=[[SkillInformation alloc] init];
        SkillInformation * skill_Q=[[SkillInformation alloc] init];
        SkillInformation * skill_W=[[SkillInformation alloc] init];
        SkillInformation * skill_E=[[SkillInformation alloc] init];
        SkillInformation * skill_R=[[SkillInformation alloc] init];
        [skill_B setValuesForKeysWithDictionary:[sourceDic objectForKey:[NSString stringWithFormat:@"%@_B",heroInformationDetailVC.hero.enName]]];
        [skill_Q setValuesForKeysWithDictionary:[sourceDic objectForKey:[NSString stringWithFormat:@"%@_Q",heroInformationDetailVC.hero.enName]]];
        [skill_W setValuesForKeysWithDictionary:[sourceDic objectForKey:[NSString stringWithFormat:@"%@_W",heroInformationDetailVC.hero.enName]]];
        [skill_E setValuesForKeysWithDictionary:[sourceDic objectForKey:[NSString stringWithFormat:@"%@_E",heroInformationDetailVC.hero.enName]]];
        [skill_R setValuesForKeysWithDictionary:[sourceDic objectForKey:[NSString stringWithFormat:@"%@_R",heroInformationDetailVC.hero.enName]]];
        [heroInformationDetailVC.heroInformationDataDic setObject:skill_B forKey:@"skill_B"];
        [heroInformationDetailVC.heroInformationDataDic setObject:skill_Q forKey:@"skill_Q"];
        [heroInformationDetailVC.heroInformationDataDic setObject:skill_W forKey:@"skill_W"];
        [heroInformationDetailVC.heroInformationDataDic setObject:skill_E forKey:@"skill_E"];
        [heroInformationDetailVC.heroInformationDataDic setObject:skill_R forKey:@"skill_R"];
//        ③.处理搭档英雄和克制英雄
        PartnerOfHero * partnerOfHero_Like_1=[[PartnerOfHero alloc] init];
        PartnerOfHero * partnerOfHero_Like_2=[[PartnerOfHero alloc] init];
        PartnerOfHero * partnerOfHero_Hate_1=[[PartnerOfHero alloc] init];
        PartnerOfHero * partnerOfHero_Hate_2=[[PartnerOfHero alloc] init];
        [partnerOfHero_Like_1 setValuesForKeysWithDictionary:[[sourceDic objectForKey:@"like"] firstObject]];
        [partnerOfHero_Like_2 setValuesForKeysWithDictionary:[[sourceDic objectForKey:@"like"] lastObject]];
        [partnerOfHero_Hate_1 setValuesForKeysWithDictionary:[[sourceDic objectForKey:@"hate"] firstObject]];
        [partnerOfHero_Hate_2 setValuesForKeysWithDictionary:[[sourceDic objectForKey:@"hate"] lastObject]];
        [heroInformationDetailVC.heroInformationDataDic setObject:partnerOfHero_Like_1 forKey:@"partnerOfHero_Like_1"];
        [heroInformationDetailVC.heroInformationDataDic setObject:partnerOfHero_Like_2 forKey:@"partnerOfHero_Like_2"];
        [heroInformationDetailVC.heroInformationDataDic setObject:partnerOfHero_Hate_1 forKey:@"partnerOfHero_Hate_1"];
        [heroInformationDetailVC.heroInformationDataDic setObject:partnerOfHero_Hate_2 forKey:@"partnerOfHero_Hate_2"];
        NSLog(@"heroInformationDataDic:%@",heroInformationDetailVC.heroInformationDataDic);
    }];
    return ;
}

#pragma mark---创建introductionView并作为默认视图
- (void) createIntroductionView{
//   创建introductionView
    self.introductionView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth([[UIScreen mainScreen] bounds]), CGRectGetHeight(self.containerView.frame)+70)];
//    将introductionView作为默认视图
    [self.containerView addSubview:self.introductionView];
//    设置introductionView的背景颜色
    self.introductionView.backgroundColor=[UIColor redColor];
    return ;
}

#pragma mark---创建skillView,暂不使用
- (void) createSkillView{
//   创建skillView
    self.skillView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth([[UIScreen mainScreen] bounds]), CGRectGetHeight(self.containerView.frame)+70)];
//    设置skillView的背景颜色
    self.skillView.backgroundColor=[UIColor greenColor];
    return ;
}

#pragma mark---创建tacticScrollView,暂不使用
- (void) createTacticScrollView{
//   创建tacticScrollView
    self.tacticScrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth([[UIScreen mainScreen] bounds]), CGRectGetHeight(self.containerView.frame)+70)];
//    设置tacticScrollView的背景颜色
    self.tacticScrollView.backgroundColor=[UIColor blueColor];
    return ;
}

#pragma mark---设置UISegmentedControl添加的方法
- (void) select:(id)sender{
//    获取heroInformationSegmentControl
    UISegmentedControl * segmentedControl= (UISegmentedControl *) sender;
    switch (segmentedControl.selectedSegmentIndex) {
        case 0:
//            切换到简介
            [self goToIntroductionView];
            break;
        case 1:
//            切换到技能
            [self goToSkillView];
            break ;
        case 2:
//            切换到攻略
            [self goToTacticScrollView];
            break ;
    }
    return ;
}

#pragma mark---切换到简介
- (void) goToIntroductionView{
    if (self.skillView.superview!=nil) {
//        从技能视图切换到简介视图
        [UIView transitionFromView:self.skillView toView:self.introductionView duration:1.0 options:UIViewAnimationOptionTransitionFlipFromLeft completion:nil];
    }else if(self.tacticScrollView!=nil){
//        从攻略视图切换到简介视图
        [UIView transitionFromView:self.tacticScrollView toView:self.introductionView duration:1.0 options:UIViewAnimationOptionTransitionFlipFromLeft completion:nil];
    }else{
        return ;
    }
    return ;
}

#pragma mark---切换到技能
- (void) goToSkillView{
    if(self.introductionView.superview!=nil){
//        从简介视图切换到技能视图
        [UIView transitionFromView:self.introductionView toView:self.skillView duration:1.0 options:UIViewAnimationOptionTransitionFlipFromRight completion:nil];
    }else if(self.tacticScrollView!=nil){
//        从攻略视图切换到技能视图
        [UIView transitionFromView:self.tacticScrollView toView:self.skillView duration:1.0 options:UIViewAnimationOptionTransitionFlipFromLeft completion:nil];
    }else{
        return ;
    }
    return ;
}

#pragma mark---切换到攻略
- (void) goToTacticScrollView{
    if(self.introductionView.superview!=nil){
//        从简介视图切换到攻略视图
        [UIView transitionFromView:self.introductionView toView:self.tacticScrollView duration:1.0 options:UIViewAnimationOptionTransitionFlipFromRight completion:nil];
    }else if(self.skillView!=nil){
//        从技能视图切换到攻略视图
        [UIView transitionFromView:self.skillView toView:self.tacticScrollView duration:1.0 options:UIViewAnimationOptionTransitionFlipFromRight completion:nil];
    }else{
        return ;
    }
    return ;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
